package com.mms.entity.service.impl;

import com.mms.entity.dao.HibernateDao;
import com.mms.entity.model.Page;
import com.mms.entity.service.EntityService;
import org.hibernate.Filter;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.query.Query;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

/**
 * Created by dan3652@sina.com on 2016-12-07 16:48.
 */
public class EntityServiceImpl<PojoType> implements EntityService<PojoType>,InitializingBean {
    @Autowired(
            required = false
    )
    private HibernateDao hibernateDao;

    @Override
    public void afterPropertiesSet() throws Exception {
        if(this.hibernateDao != null) {
            Type type = this.getClass().getGenericSuperclass();
            if(type instanceof ParameterizedType) {
                ParameterizedType pt = (ParameterizedType)type;
                Type[] types = pt.getActualTypeArguments();
                Class clazz = (Class)types[0];
                this.hibernateDao.setPojoClass(clazz);
            }
        }
    }

    @Override
    public Filter enableFilter(String var1) {
        return this.hibernateDao.getCurrentSession().enableFilter(var1);
    }

    @Override
    public HibernateDao getEntityDao() {
        return this.hibernateDao;
    }

    @Override
    public PojoType get(Serializable id) {
        return (PojoType) this.hibernateDao.get(id);
    }

    @Override
    public void saveOrUpdate(Object obj) {
        this.hibernateDao.saveOrUpdate(obj);
    }

    @Override
    public Serializable save(Object obj) {
        return this.hibernateDao.save(obj);
    }

    @Override
    public List getAll() {
        return this.hibernateDao.getAll();
    }

    @Override
    public List find(String hql, Object... args) {
        return this.hibernateDao.find(hql,args);
    }

    @Override
    public List findByIn(Integer... strs) {
        return this.hibernateDao.findByIn(strs);
    }

    @Override
    public List find(int start, int end, String hql, Object... args) {
        return this.hibernateDao.find(start,end,hql,args);
    }

    @Override
    public Page pagedQuery(Page page) {
        return this.hibernateDao.pagedQuery(page);
    }

    @Override
    public Page pagedQuery(String fieldName, Object obj, Page page) {
        return this.hibernateDao.pagedQuery(fieldName,obj,page);
    }

    @Override
    public Page pagedQuery(Map<String, Object> map, Page page) {
        return this.hibernateDao.pagedQuery(map,page);
    }

    @Override
    public Page pagedQuery(String hql, Page page, Object... objs) {
        return this.hibernateDao.pagedQuery(hql,page,objs);
    }

    @Override
    public Page pagedQuery(Page page, DetachedCriteria criteria) {
        return this.hibernateDao.pagedQuery(page,criteria);
    }

    @Override
    public Page pageQueryBySQL(String sql, Page page) {
        return this.hibernateDao.pageQueryBySQL(sql,page);
    }

    @Override
    public Page pageQueryBySQL(String sql, Page page, Object... objs) {
        return this.hibernateDao.pageQueryBySQL(sql,page,objs);
    }

    @Override
    public Long getCountBy(String var1, Object var2) {
        return this.hibernateDao.getCountBy(var1, var2);
    }

    @Override
    public Long getCount(String var1, Object... var2) {
        return this.hibernateDao.getCount(var1, var2);
    }

    @Override
    public Long getCount(String var1) {
        return this.hibernateDao.getCount(var1);
    }

    @Override
    public Long getCount(DetachedCriteria var1) {
        return this.hibernateDao.getCount(var1);
    }

    @Override
    public Long getCount(Map<String, Object> var1) {
        return this.hibernateDao.getCount(var1);
    }

    @Override
    public int bulkUpdate(String hql) {
        return this.hibernateDao.bulkUpdate(hql);
    }

    @Override
    public int bulkUpdate(String hql, Object obj) {
        return this.hibernateDao.bulkUpdate(hql, obj);
    }

    @Override
    public int bulkUpdate(String hql, Object... obj) {
        return this.hibernateDao.bulkUpdate(hql, obj);
    }

    @Override
    public SQLQuery createSQLQuery(String sql) {
        return this.hibernateDao.createSQLQuery(sql);
    }

    @Override
    public Query createQuery(String hql) {
        return this.hibernateDao.createQuery(hql);
    }

    public Object getObject(DetachedCriteria detachedCriteria) {
        return this.hibernateDao.getObject(detachedCriteria);
    }

    public Object getObject(String hql) {
        return this.hibernateDao.getObject(hql, new Object[0]);
    }

    public Object getObject(String hql, Object... args) {
        return this.hibernateDao.getObject(hql, args);
    }

    public PojoType getObjectBy(String fieldName, Object obj) {
        return (PojoType) this.hibernateDao.getObjectBy(fieldName, obj);
    }

    public PojoType getObjectBy(Map<String, Object> map) {
        return (PojoType) this.hibernateDao.getObjectBy(map);
    }
}
